package com.chase.passwordmaster.mine.export

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import toJson
import java.io.BufferedWriter
import java.io.File
import java.io.FileWriter
import java.lang.Exception

fun export(dir: String, fileName: String, content: Any) {
    val dirsFile = File(dir)
    if (!dirsFile.exists()) {
        dirsFile.mkdirs()
    }

    val file = File(dirsFile.toString(), fileName)

    try {
        file.createNewFile()
    } catch (e: Exception) {
    }

    try {
        val writer = BufferedWriter(FileWriter(file, false))
        writer.write(content.toJson())
        writer.close()
    } catch (e: Exception) {
    }
}

fun importExternalAccountList(json: String): List<ExternalDecryptedAccount> =
    Gson().fromJson(
        json,
        object : TypeToken<List<ExternalDecryptedAccount>>() {}.type
    )
